

$(document).ready(function(){
	var currentPage=1;
	var pageSize=10;
	var token=$.zui.store.get("token");
	var userId=$.zui.store.get("userId");
	var queryMerchantList =function(){
		var queryListUrl= adminQueryUrl+merchantService;
		var param="action=list";
	
		if($("#queryMerchantCode").val()!=''){
			   param +="&MerchantCode="+$("#queryMerchantCode").val();
		}
		if($("#queryMerchantNameLike").val()!=''){
			 param +="&queryMerchantNameLike="+$("#queryMerchantNameLike").val();
		}
		param +="&pageSize="+pageSize;
		param +="&currentPage="+(currentPage-1);
		$.ajax({
			url:queryListUrl,
			type:"post",
			beforeSend:function(xhr){
				xhr.setRequestHeader("token",token);
				xhr.setRequestHeader("userId",userId);
			},
			header:{"token":token,"userId":userId},
			data:param,
			dataType:"JSON",
			success:function(result){
				console.log(result.merchantList);
				
				if (result.success=='1'){
					
					$('#dataGridList').empty();
					$('#dataGridList').data('zui.datagrid',null);
					$('#dataGridList').datagrid({
						checkable:true,
				        checkByClickRow: true,
					    dataSource: {
					        cols:[
					        	 {name: 'merchantId', label: '商户编号', width: 132},
					            {name: 'merchantCode', label: '商户编码', width: 132},
					            {name: 'merchantName', label: '商户名称', width: 134},
					  
					        
					
					        ],
					      array:result.merchantList
					    }
					});	
	     
					// 手动进行初始化
	           $('#myPager').empty();
	       	$('#myPager').data('zui.pager',null);
	    
	    	
					$('#myPager').pager({
						
					    page: currentPage,
					    recPerPage:pageSize,
					    elements:['first_icon', 'prev_icon', 'pages', 'next_icon', 'last_icon', 'total_text','size_menu','goto'],	
					    pageSizeOptions:[5,10,20,30,50,100],
					    recTotal: result.totalCount
					});
			}
				else{
					new $.zui.Messager(result.msg, {
					    type: 'warning', // 定义颜色主题
					    placement: 'center' // 定义显示位置
					}).show();
					}
				},
				error: function (jqXHR, textStatus, errorThrown) {
		            /*弹出jqXHR对象的信息*/
		            alert(jqXHR.responseText);
		            alert(jqXHR.status);
		            alert(jqXHR.readyState);
		            alert(jqXHR.statusText);
		            /*弹出其他两个参数的信息*/
		            alert(textStatus);
		            alert(errorThrown);
				},
				error:function(result){
					new $.zui.Messager('系统繁忙,请稍候再试!', {
					    type: 'warning', // 定义颜色主题
					    placement: 'center' // 定义显示位置
					}).show();
				}
			
			
		});
	}
	
	queryMerchantList();
	$("#searchBtn").click(function(){
		queryMerchantList();
	});
	$('#myPager').on('onPageChange', function(e, state, oldState) {
		currentPage = state.page;
		
		pageSize = state.recPerPage;
		queryMerchantList();
	});
	$("#addBtn").click(function(){
//		$('#addModal').modal('show', 'fit');
		$("#addForm")[0].reset();
	/*	var url = adminQueryUrl+orgService+"?action=initAdd";
		

		
		$.ajax({
			url:url,
			type:'post',
			dataType:'JSON',
			beforeSend:function(xhr){
				xhr.setRequestHeader("token",token);
				xhr.setRequestHeader("userId",userId);
			},
			header:{"token":token,"userId":userId},
		
			success:function(result){
				
				if (result.success=='1'){
					console.log('data='+result.orgList);
					$.each(result.orgList, function(i, item){     
						
						$("#addOrgParentCode").append("<option value='"+item.orgCode+"'>"+item.orgName+"</option>");
					});  
				}
			},
			error:function(e){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning',
				    placement:'center'
				}).show();
			}
			
		});*/
		
	});
		
	$("#addSaveBtn").click(function(){
	
//		var validMenuCodeUrl = adminQueryUrl + menuService + "?action=validMenuCode";
//		var validMenuNameUrl = adminQueryUrl + menuService + "?action=validMenuName";
//       alert("2="+validMenuCodeUrl);
		$("#addForm").validate({
			rules:{
				addMerchantCode:{
					"required":true/*,
					"remote":{
						url:validMenuCodeUrl,
						type:'get',
						dataType:'json',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token",token);
							xhr.setRequestHeader("userId",userId);
						},
						headers:{'token':token,'userId':userId},
						data:{
							'MenuCode':function(){
								return $("#addMenuCode").val();
							}
						}
					}*/
				},
				addMerchantName:{
					"required":true/*,
					"remote":{
						url:validMenuNameUrl,
						type:'get',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token",token);
							xhr.setRequestHeader("userId",userId);
						},
						headers:{'token':token,'userId':userId},
						dataType:'json',
						data:{
							'MenuName':function(){
								return $("#addMenuName").val();
							},
							'MenuParentCode':function(){
								return $("#addMenuParentCode").val();
							}
						}
					}*/
				},
				
			},
			messages:{
				addMerchantCode:{
					"required":"请输入菜单编码"/*,
					"remote":"菜单编码已经存在"*/
				},
				addMerchantName:{
					"required":"请输入菜单名称"/*,
					"remote":"菜单名称已经存在"*/
				}
			},
			submitHandler:function(form){
				var addurl=adminQueryUrl + merchantService;
				alert(token+",userID="+userId)
				$.ajax({
					url:addurl,
					type:'post',
					dataType:'JSON',
					beforeSend:function(xhr){//设置请求头信息
						xhr.setRequestHeader("token",token);
						xhr.setRequestHeader("userId",userId);
					},
					headers:{'token':token,'userId':userId},

					data:$(form).serialize(),
					success:function(data){
						console.log('data='+data);
						if (data.success == '1') {
		                	new $.zui.Messager('添加成功!', {
		    				    type: 'success',
		    				    placement:'center'
		    				}).show();
		                	$('#addModal').modal('hide', 'fit');
		                	queryMerchantList();
		                }
		                else{
		                	new $.zui.Messager(data.msg, {
		    				    type: 'warning',
		    				    placement:'center'
		    				}).show();
		                }
					},
//			error: function (jqXHR, textStatus, errorThrown) {
//			            /*弹出jqXHR对象的信息*/
//			            alert(jqXHR.responseText);
//			            alert(jqXHR.status);
//			            alert(jqXHR.readyState);
//			            alert(jqXHR.statusText);
//			            /*弹出其他两个参数的信息*/
//			            alert(textStatus);
//			            alert(errorThrown);
//					},
					error:function(e){
						new $.zui.Messager('系统繁忙,请稍候再试!', {
	    				    type: 'warning',
	    				    placement:'center'
	    				}).show();
					}
				});
			}
			
		});
		
		
	});
	
	$("#deleteBtn").click(function(){
		// 获取数据表格实例
		var DataGrid = $('#dataGridList').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = DataGrid.getCheckItems();
		if (selectedItems.length == 0) {
			new $.zui.Messager('请选择要删除的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else if (selectedItems.length > 1) {
			new $.zui.Messager('请只选择一条要删除的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else {
	
			$('#deleteModal').modal('show', 'fit');
		
		}
	});
	$("#deleteSure").click(function(){
		// 获取数据表格实例
		var DataGrid = $('#dataGridList').data('zui.datagrid');
		// 获取当前已选中的行数据项
		
		
		var selectedItems = DataGrid.getCheckItems();
		
		var url = adminQueryUrl + merchantService;
		
		var param = 'action=delete&merchantCode='+selectedItems[0].merchantCode;
		param += '&merchantId='+selectedItems[0].merchantId;//请求到列表页面
		console.log('param2='+param);
		$.ajax({
			url:url,
			type:'post',
			dataType:'JSON',
			beforeSend:function(xhr){//设置请求头信息
				xhr.setRequestHeader("token",token);
				xhr.setRequestHeader("userId",userId);
			},
			headers:{'token':token,'userId':userId},
			data:param,
			success:function(result){
				console.log('data='+result);
				$('#deleteModal').modal('hide', 'fit');
				if (result.success=='1'){
	                	new $.zui.Messager('删除成功!', {
	    				    type: 'success',
	    				    placement:'center'
	    				}).show();
	                	queryMerchantList();
				}
                else{
                	new $.zui.Messager(result.msg, {
    				    type: 'warning',
    				    placement:'center'
    				}).show();
				}
			},
			error:function(e){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning',
				    placement:'center'
				}).show();
			}
		});
	});
	
	
	
	
	$("#updateBtn").click(function(){
		var DataGrid = $('#dataGridList').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = DataGrid.getCheckItems();
		if (selectedItems.length == 0) {
			new $.zui.Messager('请选择要修改的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else if (selectedItems.length > 1) {
			new $.zui.Messager('请只选择一条要修改的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else {
			$("#updateForm")[0].reset();
			var url=adminQueryUrl+merchantService;
			var param="action=getUpdateMerchant&merchantId="+selectedItems[0].merchantId;
			console.log('param2='+param);
			$.ajax({
				url:url,
				data:param,
				dataType:'json',
				beforeSend:function(xhr){
					xhr.setRequestHeader("token",token);
					xhr.setRequestHeader("userId",userId);
				},
				headers:{
					'token':token,
					'userId':userId
				},
				success:function(result){
					if(result.success=="1"){
						console.log(result);
						$("#updateModal").modal('show','fit');
						$("#updateMerchantCode").val(result.merchantList.merchantCode);
						$("#updateMerchantName").val(result.merchantList.merchantName);

						$("#updateMerchantId").val(result.merchantList.merchantId);
					}
				},
				error:function(e){
					new $.zui.Messager('系统繁忙,请稍候再试!', {
					    type: 'warning',
					    placement:'center'
					}).show();
				}
				
			});
		}
	});
	
	$("#updateSaveBtn").click(function(){
		/*var validMenuCodeUrl = adminQueryUrl + menuService + "?action=validMenuCode";
		var validMenuNameUrl = adminQueryUrl + menuService + "?action=validMenuName";
       alert("2="+validMenuCodeUrl);*/
		$("#updateForm").validate({
			rules:{
				updateMerchantCode:{
					"required":true/*,
					"remote":{
						url:validMenuCodeUrl,
						type:'get',
						dataType:'json',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						data:{
							'MenuCode':function(){
								return $("#updateMenuCode").val();
							},
							'excludeMenuId':function(){
								return $("#updateMenuId").val();
							}
						}
					}*/
				},
				updateMerchantName:{
					"required":true/*,
					"remote":{
						url:validMenuNameUrl,
						type:'get',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						dataType:'json',
						data:{
							'MenuName':function(){
								return $("#updateMenuName").val();
							},
							'MenuParentCode':function(){
								return $("#updateMenuParentCode").val();
							},
							'excludeMenuId':function(){
								return $("#updateMenuId").val();
							}
						}
					}*/
				},
				
			},
			messages:{
				updateMerchantCode:{
					"required":"请输入商户编码"/*,
					"remote":"菜单编码已经存在"*/
				},
				updateMerchantName:{
					"required":"请输入商户名称"/*,
					"remote":"菜单名称已经存在"*/
				}
			},
			submitHandler:function(form){
				var updateurl=adminQueryUrl + merchantService+"?action=update";
				
				$.ajax({
					url:updateurl,
					type:'post',
					dataType:'JSON',
					beforeSend:function(xhr){//设置请求头信息
						xhr.setRequestHeader("token",token);
						xhr.setRequestHeader("userId",userId);
					},
					headers:{'token':token,'userId':userId},
				
					data:$(form).serialize(),
					success:function(data){
						console.log('data='+data);
						if (data.success == '1') {
		                	new $.zui.Messager('修改成功!', {
		    				    type: 'success',
		    				    placement:'center'
		    				}).show();
		                	$('#updateModal').modal('hide', 'fit');
		                	queryMerchantList();
		                }
		                else{
		                	new $.zui.Messager(data.msg, {
		    				    type: 'warning',
		    				    placement:'center'
		    				}).show();
		                }
					},

					error:function(e){
						new $.zui.Messager('系统繁忙,请稍候再试!', {
	    				    type: 'warning',
	    				    placement:'center'
	    				}).show();
					}
				});
			}
			
		});
	});
	$("#detailBtn").click(function(){
	
		var DataGrid = $('#dataGridList').data('zui.datagrid');
		// 获取当前已选中的行数据项
		var selectedItems = DataGrid.getCheckItems();
		if (selectedItems.length == 0) {
			new $.zui.Messager('请选择要查看的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else if (selectedItems.length > 1) {
			new $.zui.Messager('请只选择一条要查看的记录', {
			    type: 'warning',
			    placement:'center'
			}).show();
		}
		else {
			$("#detailForm")[0].reset();
			var url=adminQueryUrl+merchantService;
			var param="action=getUpdateMerchant&merchantId="+selectedItems[0].merchantId;
			console.log('param2='+param);
			$.ajax({
				url:url,
				data:param,
				dataType:'json',
				beforeSend:function(xhr){
					xhr.setRequestHeader("token",token);
					xhr.setRequestHeader("userId",userId);
				},
				headers:{
					'token':token,
					'userId':userId
				},
				success:function(result){
					if(result.success=="1"){
						console.log(result);
						$("#detailModal").modal('show','fit');
						$("#MerchantCode").val(result.merchantList.merchantCode);
						$("#MerchantName").val(result.merchantList.merchantName);

					}
				},
				error:function(e){
					new $.zui.Messager('系统繁忙,请稍候再试!', {
					    type: 'warning',
					    placement:'center'
					}).show();
				}
				
			});
		}
		
		
	});
	
});



